
package com.lczyfz.campusinfo.modules.frontuser.web;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.modules.frontuser.entity.CampusinfoFrontUser;
import com.lczyfz.campusinfo.modules.frontuser.service.CampusinfoFrontUserService;
import com.lczyfz.campusinfo.modules.sys.service.SystemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 前端用户管理Controller
 *
 * @author yt
 * @version 2017-10-26
 */
@Controller
@RequestMapping(value = "${adminPath}/frontuser/campusinfoFrontUser")
public class CampusinfoFrontUserController extends BaseController {

    @Autowired
    private SystemService systemService;

    @Autowired
    private CampusinfoFrontUserService campusinfoFrontUserService;

    @ModelAttribute
    public CampusinfoFrontUser get(@RequestParam(required = false) String id) {
        CampusinfoFrontUser entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = campusinfoFrontUserService.get(id);
        }
        if (entity == null) {
            entity = new CampusinfoFrontUser();
        }
        return entity;
    }

    @RequiresPermissions("frontuser:campusinfoFrontUser:view")
    @RequestMapping(value = {"list", ""})
    public String list(CampusinfoFrontUser campusinfoFrontUser, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<CampusinfoFrontUser> page = campusinfoFrontUserService.findPage(new Page<CampusinfoFrontUser>(request, response), campusinfoFrontUser);
        model.addAttribute("page", page);
        return "modules/frontuser/campusinfoFrontUserList";
    }

    @RequiresPermissions("frontuser:campusinfoFrontUser:view")
    @RequestMapping(value = "form")
    public String form(CampusinfoFrontUser campusinfoFrontUser, Model model) {
        model.addAttribute("campusinfoFrontUser", campusinfoFrontUser);
        model.addAttribute("allRoles", systemService.findAllRole());
        return "modules/frontuser/campusinfoFrontUserForm";
    }

    @RequiresPermissions("frontuser:campusinfoFrontUser:edit")
    @RequestMapping(value = "save")
    public String save(CampusinfoFrontUser campusinfoFrontUser, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, campusinfoFrontUser)) {
            return form(campusinfoFrontUser, model);
        }
        campusinfoFrontUserService.save(campusinfoFrontUser);
        addMessage(redirectAttributes, "保存前端用户信息成功");
        return "redirect:" + Global.getAdminPath() + "/frontuser/campusinfoFrontUser/?repage";
    }

    @RequiresPermissions("frontuser:campusinfoFrontUser:edit")
    @RequestMapping(value = "delete")
    public String delete(CampusinfoFrontUser campusinfoFrontUser, RedirectAttributes redirectAttributes) {
        campusinfoFrontUserService.delete(campusinfoFrontUser);
        addMessage(redirectAttributes, "删除前端用户信息成功");
        return "redirect:" + Global.getAdminPath() + "/frontuser/campusinfoFrontUser/?repage";
    }

}